//
//  AYSCategorySmallViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/2.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSCategorySmallViewController.h"
#import "AYSCategorySmallListHeadView.h"
#import "AYSRequestManager.h"
#import "AYSSmallListModel.h"
#import "AYSSmallListTableViewCell.h"
#import "AYSCategorySmallDeViewController.h"
#import "MJRefresh.h"
#import "SVProgressHUD.h"
@interface AYSCategorySmallViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *videoListMArray;
@property (nonatomic, strong) NSString *nextPageUrl;
@property (nonatomic, strong) AYSCategorySmallListHeadView *headerView;
@property(nonatomic, strong)UITableView *mainTableView;
@end

@implementation AYSCategorySmallViewController
- (NSMutableArray *)videoListMArray
{
    if (!_videoListMArray)
    {
        self.videoListMArray = [[NSMutableArray alloc] init];
    }
    return _videoListMArray;
}

#pragma wark ===============网络请求
- (void)reloadVideoListWithUrl:(NSString *)url isNil:(BOOL)isNil
{
    [AYSRequestManager requestWithType:GET url:url parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        if (isNil)
        {
            self.videoListMArray = nil;
        }
        self.nextPageUrl = dic[@"nextPageUrl"];
        for (NSDictionary *videoList in dic[@"videoList"])
        {
            AYSSmallListModel *model = [AYSSmallListModel shareJsonWithDic2:videoList dic3:videoList[@"consumption"] dic4:videoList[@"provider"] arr:videoList[@"playInfo"]];
            [self.videoListMArray addObject:model];
        }
        [SVProgressHUD dismiss];
        [_mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"小视频列表网络请求失败");
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    UIColor *color = [UIColor whiteColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.032 alpha:1.000];
    self.titleLabel.text = self.classNameStr;
    self.titleLabel.frame = CGRectMake(SCREEN_WIDTH / 2 - 10, 30, 100, 30);
    self.classURLStr = [[kClassUrl stringByAppendingString:self.classNameStr] stringByAppendingString:kClassUrldate];
    self.classURLStr = [_classURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [SVProgressHUD show];
    [self reloadVideoListWithUrl:_classURLStr isNil:YES];
    [self getMainTableView];
    
    
    
}


#pragma wark=================刷新数据
- (void)refreshData
{
    __weak UITableView *tableView = self.mainTableView;
    // 下拉刷新
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadVideoListWithUrl:self.classURLStr isNil:YES];
       
        [tableView.header endRefreshing];
        
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.header.automaticallyChangeAlpha = YES;
    
    // 上拉刷新
    tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.nextPageUrl != NULL)
        {
            [self reloadVideoListWithUrl:self.nextPageUrl isNil:NO];
        }
    [tableView.footer endRefreshing];
        
    }];

}


// 创建tableView
- (void)getMainTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _mainTableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_mainTableView];
    
    // 注册
    [_mainTableView registerClass:[AYSSmallListTableViewCell class] forCellReuseIdentifier:@"reuse"];
    // 刷新数据
    [self refreshData];
    
    
}

#pragma wark ============tableView dataSource  delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoListMArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSSmallListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    AYSSmallListModel *model = self.videoListMArray[indexPath.row];
    cell.ranking = indexPath.row;
    [cell setValueWithModel:model];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT / 5 * 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

// 区头设置
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    _headerView.backgroundColor = [UIColor whiteColor];
    if (self.headerView == nil)
    {
        NSArray *array = @[@"按时间排序", @"分享排行榜"];
        self.headerView = [[AYSCategorySmallListHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) array:array];
        [self addTagetForBtnWithCount:array.count view:_headerView];
    }
    return _headerView;
}

- (void)addTagetForBtnWithCount:(NSUInteger)count view:(UIView *)view
{
    for (int i = 0; i < count; i++)
    {
        UIButton *button = (UIButton *)[self.headerView viewWithTag:1100 + i];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    }
}

- (void)btnClick:(UIButton *)button
{
    if (button.tag == 1100)
    {
        self.headerView.timebtn.selected = YES;
        self.headerView.timebtn.userInteractionEnabled = NO;
        self.headerView.sharebtn.selected = NO;
        self.headerView.sharebtn.userInteractionEnabled = YES;
        self.videoListMArray = nil;
        self.classURLStr = [[kClassUrl stringByAppendingString:self.classNameStr] stringByAppendingString:kClassUrldate];
        _classURLStr = [_classURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self reloadVideoListWithUrl:_classURLStr isNil:YES];
    }
    else if (button.tag == 1101)
    {
        self.headerView.timebtn.selected = NO;
        self.headerView.timebtn.userInteractionEnabled = YES;
        self.headerView.sharebtn.selected = YES;
        self.headerView.sharebtn.userInteractionEnabled = NO;
        self.videoListMArray = nil;
        self.classURLStr = [[kClassUrl stringByAppendingString:self.classNameStr] stringByAppendingString:kClassUrlshareCount];
        _classURLStr = [_classURLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self reloadVideoListWithUrl:_classURLStr isNil:YES];
    }
}

#pragma wark ================添加每个cell出现时的动画（从左边出现）
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D rotation;// 3D旋转
    rotation.m34 = 1.0 / -600;// 逆时针旋转
    
    
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.contentView.layer.shadowOffset = CGSizeMake(0, 0);
    cell.alpha = 0;
    
    cell.contentView.layer.transform = rotation;
    cell.contentView.layer.anchorPoint = CGPointMake(0, 0.5);// 旋转定点
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.contentView.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.contentView.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}

// 进入小视频详情界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSCategorySmallDeViewController *smallDetailVC = [[AYSCategorySmallDeViewController alloc] init];
    smallDetailVC.model = self.videoListMArray[indexPath.row];
    [self.navigationController pushViewController:smallDetailVC animated:YES];
}


//// 实现topView的渐变
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    CGFloat y = scrollView.contentOffset.y;
//    if (y > -63 && y <= 37)
//    {
//        self.topView.alpha = (y + 63) / 100;
//    }else if (y > -63)
//    {
//        self.topView.alpha = 0;
//        
//    }else
//    {
//        self.topView.alpha = 1;
//    }
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
